package com.lab.servlet;



import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.StudentDao;
import com.lab.dao.impl.StudentDaoImpl;
import com.lab.po.PageBean;
import com.lab.po.Student;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class ShowStudentAjax
 */
@WebServlet("/showStudentAjax")
public class ShowStudentAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowStudentAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		int currentPage=1;
		if(request.getParameter("currentPage")!=null){
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		
		//ajax请求

			StudentDao studentdao=new StudentDaoImpl();
			PageBean pagebean=new PageBean();
			pagebean.setCurrentPage(currentPage);
			
			String stuid=request.getParameter("stuid");
			String stuname=request.getParameter("stuname");
			String maj_name=request.getParameter("maj_name");
			String address=request.getParameter("address");

			Map map=new HashMap();
			if(stuid!=null && !stuid.equals("")){
				map.put("stuid", "%"+stuid+"%");
			}
			if(stuname!=null && !stuname.equals("")){
				map.put("stuname", "%"+stuname+"%");
			}
			if(maj_name!=null && !maj_name.equals("")){
				map.put("maj_name", "%"+maj_name+"%");
			}
			if(address!=null && !address.equals("")){
				map.put("address", "%"+address+"%");
			}
			pagebean.setCountRecords(studentdao.countStudentByCondition(map));
			map.put("key1", (currentPage-1)*pagebean.getCountPage());
			map.put("key2",pagebean.getCountPage());
			List<Student> studentList=studentdao.selectStudentByCon(map);
			JSONArray jsonArray=JSONArray.fromObject(studentList);
			response.getWriter().write(jsonArray.toString());
	}

}
